package com.gxa.sso.service.impl;

import com.gxa.sso.exception.CustomerException;
import com.gxa.sso.service.SmsService;
import com.gxa.sso.utils.JedisTemplete;
import com.gxa.sso.utils.SendSms;
import jdk.nashorn.api.scripting.ScriptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * Created by zxd on 2020/12/16
 **/
@Service
public class SmsServiceImpl implements SmsService {


    @Autowired
    private JedisTemplete jedisTemplete;

    public static final String LOGIN_SMS="login_sms:";


    @Override
    public void sendLoginSms(String phone) throws CustomerException {

        //一键登录 有没有账号都可以发送短信
        //发送登录  没有注册账号就不行
        //发送注册短信 注册了就不行

        //1.生成验证码
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<6;i++){
            sb.append(new Random().nextInt(10));
        }
        //生成的验证码
        String code=sb.toString();

        System.out.println(code+"-----------------");

        //2.服务器存储验证码
        jedisTemplete.set(LOGIN_SMS+phone,code);

        //3.设置失效时间
        jedisTemplete.expire(LOGIN_SMS+phone,60*5);

        //4.发送短信给用户
        //SendSms.sendSms(phone,code);

    }
}
